#include "databasefacade.h"
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QSqlError>
#include <QStringList>

DataBaseFacade::DataBaseFacade()
{
    sd = new CSSettingsInformation;
    Users = new QList<User *>;
    PTList = new QList<ProtoolsStandard *>;
}

DataBaseFacade::~DataBaseFacade()
{
    delete sd;
    delete Users;
    delete PTList;
}

bool DataBaseFacade::TestConnection(ComResponse *resp)
{
    bool retval = true;

    if (!resp)
    {
        resp = new ComResponse;
    }

    QStringList lst = QSqlDatabase::drivers();

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        resp->ErrorCode = ComResponse::NO_FAILURE;
        resp->ErrorMessage = "Open Database";
        qd.close();
    }
    else
    {
        retval = false;
        resp->ErrorCode = ComResponse::CANNOT_CONNECT_TO_DATABASE;
        QSqlError err = qd.lastError();
        resp->ErrorMessage = err.databaseText();
    }

    QSqlDatabase::removeDatabase("QODBC");

    return retval;
}

bool DataBaseFacade::OpenConnection(QSqlDatabase &qd)
{
    bool retval = true;

    qd = QSqlDatabase::addDatabase("QODBC");

    if (sd->GetDatabaseName() != "")
    {
        qd.setDatabaseName(sd->GetDatabaseName());
    }

    if (sd->GetDatabaseName() != "")
    {
        qd.setHostName(sd->GetDataaseAddress());
    }

    if (sd->GetDatabaseName() != "")
    {
        qd.setPassword(sd->GetDatabasePassword());
    }

    if (sd->GetDatabaseName() != "")
    {
        qd.setPort(sd->GetDatabasePort().toInt());
    }

    if (sd->GetDatabaseName() != "")
    {
        qd.setUserName(sd->GetDatabaseUser());
    }

    if (qd.open())
    {
        retval = true;
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::CloseConnection(QSqlDatabase &qd)
{
    qd.close();
    QSqlDatabase::removeDatabase("QODBC");
    return true;
}

bool DataBaseFacade::AuthUserByUserName(const QString &username, QString userpassword)
{
    bool retval = false;

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("select UserPassword, UserName from users where UserName = ?");
        query.addBindValue(QVariant(username));
        query.exec();
        while (query.next())
        {
            QString tpassword = query.value(0).toString();
            QString tusername = query.value(1).toString();

            if (username == tusername && tpassword == userpassword)
            {
                retval = true;
            }
            else
            {
                retval = false;
            }
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

void DataBaseFacade::GetAllUsers()
{
    this->Users->clear();
    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("SELECT FirstName, LastName, MiddleName, UserID, UserIsActive, UserName, UserPassword from users");
        query.exec();
        while (query.next())
        {
            User *u = new User;
            u->FirstName = query.value(0).toString();
            u->LastName = query.value(1).toString();
            u->MiddleName = query.value(2).toString();
            u->UserID = query.value(3).toInt();
            u->UserIsActive = query.value(4).toBool();
            u->UserName = query.value(5).toString();
            u->UserPassword = query.value(6).toString();
            Users->append(u);
        }

        this->CloseConnection(qd);
    }
}

bool DataBaseFacade::GetUserByUserID(int userID, User *u)
{
    bool retval = false;

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("select FirstName, LastName, MiddleName, UserID, UserIsActive, UserName, UserPassword from users where userID = ?");
        query.addBindValue(QVariant(userID));
        query.exec();
        while (query.next())
        {
            u->FirstName = query.value(0).toString();
            u->LastName = query.value(1).toString();
            u->MiddleName = query.value(2).toString();
            u->UserID = query.value(3).toInt();
            u->UserIsActive = query.value(4).toBool();
            u->UserName = query.value(5).toString();
            u->UserPassword = query.value(6).toString();
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::InsertUser(User *u)
{
    bool retval = false;

    if (!u)
    {
        return retval;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("INSERT INTO users (FirstName, LastName, MiddleName, UserIsActive, UserName, UserPassword) values (?, ?, ?, ?, ?, ?)");
        query.addBindValue(QVariant(u->FirstName));
        query.addBindValue(QVariant(u->LastName));
        query.addBindValue(QVariant(u->MiddleName));
        query.addBindValue(QVariant(u->UserIsActive));
        query.addBindValue(QVariant(u->UserName));
        query.addBindValue(QVariant(u->UserPassword));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::UpdateUser(User *u)
{
    bool retval = false;

    if (!u)
    {
        return retval;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("Update users set FirstName = ?, LastName = ?, MiddleName = ?, UserIsActive = ?, UserName = ?, UserPassword = ? where UserID = ?");
        query.addBindValue(QVariant(u->FirstName));
        query.addBindValue(QVariant(u->LastName));
        query.addBindValue(QVariant(u->MiddleName));
        query.addBindValue(QVariant(u->UserIsActive));
        query.addBindValue(QVariant(u->UserName));
        query.addBindValue(QVariant(u->UserPassword));
        query.addBindValue(QVariant(u->UserID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::DeleteUser(User *u)
{
    bool retval = false;

    if (!u)
    {
        return retval;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("delete from users where UserID = ?");
        query.addBindValue(QVariant(u->UserID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::UpdateProductionByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("update Productions set Main_lblSize = ?,Main_lblVenues = ? ,Main_lblArtists = ? ,Main_lblPerformaceDates = ? ,Main_lblProdCost = ? ,Main_lblStaffCost = ? ,Main_lblAdminCosts = ? ,Main_lblMktgCosts = ? ,Main_lblSecCosts = ? ,Main_lblOpsCost = ? ,Main_lblSubTotal = ? ,Main_TicketCount = ? ,Main_lblTicketSalesPos = ? ,Main_lblPossibleDollars = ? ,Main_lblPossibleTicletCOmm = ? ,Main_lblTax = ? ,Main_lblNet = ? ,AdminScreen_AdminStaff = ? ,AdminScreen_BoxOffStaff = ? ,AdminScreen_TicketTakers = ? ,AdminScreen_TrafficControl = ? ,AdminScreen_OfficeCosts = ? ,AdminScreen_OfficeEquipment = ? ,AdminScreen_Postage = ? ,AdminScreen_Phones = ? ,AdminScreen_LegalAccountant = ? ,AdminScreen_RentPower = ? ,AdminScreen_Insurance = ? ,AdminScreen_Permits = ? ,AdminScreen_ASCAP = ? ,AdminScreen_Ticketprinting = ? ,MarketingScreen_PublicRelations = ? ,MarketingScreen_CreativePrinting = ? ,MarketingScreen_MktgStaff = ? ,OperationsScreen_SiteOpsCoordinator = ? ,OperationsScreen_Laborers = ? ,OperationsScreen_VenueCost = ? ,OperationsScreen_LandRestore = ? ,OperationsScreen_Kamode = ? ,OperationsScreen_WaterTrucks = ? ,OperationsScreen_EquipmentRent = ? ,OperationsScreen_Petroleum = ? ,OperationsScreen_PowerGenerator = ? ,OperationsScreen_PerimeterLights = ? ,OperationsScreen_Fence = ? ,OperationsScreen_OpsVehicles = ? ,OperationsScreen_ShuttleBuses = ? ,OperationsScreen_MotorHomes = ?,OperationsScreen_LandPrep = ? ,ProductionScreen_Talent = ? ,ProductionScreen_Sound = ? ,ProductionScreen_Lights = ? ,ProductionScreen_Catering = ? ,ProductionScreen_Stage = ? ,ProductionScreen_LightTowers = ? ,ProductionScreen_Aerials = ? ,SecurityScreen_SecStaff = ? ,SecurityScreen_Vehicles = ? ,SecurityScreen_FirstAid = ? ,SecurityScreen_Communications = ? ,SecurityScreen_Ids = ? ,StaffScreen_ProdStaff = ? ,StaffScreen_StageHands = ? ,TicketComm = ? ,ProductionName = ? where ProductionID = ?");
        query.addBindValue(QVariant(pto->Main_lblSize));
        query.addBindValue(QVariant(pto->Main_lblVenues));
        query.addBindValue(QVariant(pto->Main_lblArtists));
        query.addBindValue(QVariant(pto->Main_lblPerformaceDates));
        query.addBindValue(QVariant(pto->Main_lblProdCost));
        query.addBindValue(QVariant(pto->Main_lblStaffCost));
        query.addBindValue(QVariant(pto->Main_lblAdminCosts));
        query.addBindValue(QVariant(pto->Main_lblMktgCosts));
        query.addBindValue(QVariant(pto->Main_lblSecCosts));
        query.addBindValue(QVariant(pto->Main_lblOpsCost));
        query.addBindValue(QVariant(pto->Main_lblSubTotal));
        query.addBindValue(QVariant(pto->Main_TicketCount));
        query.addBindValue(QVariant(pto->Main_lblTicketSalesPos));
        query.addBindValue(QVariant(pto->Main_lblPossibleDollars));
        query.addBindValue(QVariant(pto->Main_lblPossibleTicletCOmm));
        query.addBindValue(QVariant(pto->Main_lblTax));
        query.addBindValue(QVariant(pto->Main_lblNet));
        query.addBindValue(QVariant(pto->AdminScreen_AdminStaff));
        query.addBindValue(QVariant(pto->AdminScreen_BoxOffStaff));
        query.addBindValue(QVariant(pto->AdminScreen_TicketTakers));
        query.addBindValue(QVariant(pto->AdminScreen_TrafficControl));
        query.addBindValue(QVariant(pto->AdminScreen_OfficeCosts));
        query.addBindValue(QVariant(pto->AdminScreen_OfficeEquipment));
        query.addBindValue(QVariant(pto->AdminScreen_Postage));
        query.addBindValue(QVariant(pto->AdminScreen_Phones));
        query.addBindValue(QVariant(pto->AdminScreen_LegalAccountant));
        query.addBindValue(QVariant(pto->AdminScreen_RentPower));
        query.addBindValue(QVariant(pto->AdminScreen_Insurance));
        query.addBindValue(QVariant(pto->AdminScreen_Permits));
        query.addBindValue(QVariant(pto->AdminScreen_ASCAP));
        query.addBindValue(QVariant(pto->AdminScreen_Ticketprinting));
        query.addBindValue(QVariant(pto->MarketingScreen_PublicRelations));
        query.addBindValue(QVariant(pto->MarketingScreen_CreativePrinting));
        query.addBindValue(QVariant(pto->MarketingScreen_MktgStaff));
        query.addBindValue(QVariant(pto->OperationsScreen_SiteOpsCoordinator));
        query.addBindValue(QVariant(pto->OperationsScreen_Laborers));
        query.addBindValue(QVariant(pto->OperationsScreen_VenueCost));
        query.addBindValue(QVariant(pto->OperationsScreen_LandRestore));
        query.addBindValue(QVariant(pto->OperationsScreen_Kamode));
        query.addBindValue(QVariant(pto->OperationsScreen_WaterTrucks));
        query.addBindValue(QVariant(pto->OperationsScreen_EquipmentRent));
        query.addBindValue(QVariant(pto->OperationsScreen_Petroleum));
        query.addBindValue(QVariant(pto->OperationsScreen_PowerGenerator));
        query.addBindValue(QVariant(pto->OperationsScreen_PerimeterLights));
        query.addBindValue(QVariant(pto->OperationsScreen_Fence));
        query.addBindValue(QVariant(pto->OperationsScreen_OpsVehicles));
        query.addBindValue(QVariant(pto->OperationsScreen_ShuttleBuses));
        query.addBindValue(QVariant(pto->OperationsScreen_MotorHomes));
        query.addBindValue(QVariant(pto->OperationsScreen_LandPrep));
        query.addBindValue(QVariant(pto->ProductionScreen_Talent));
        query.addBindValue(QVariant(pto->ProductionScreen_Sound));
        query.addBindValue(QVariant(pto->ProductionScreen_Lights));
        query.addBindValue(QVariant(pto->ProductionScreen_Catering));
        query.addBindValue(QVariant(pto->ProductionScreen_Stage));
        query.addBindValue(QVariant(pto->ProductionScreen_LightTowers));
        query.addBindValue(QVariant(pto->ProductionScreen_Aerials));
        query.addBindValue(QVariant(pto->SecurityScreen_SecStaff));
        query.addBindValue(QVariant(pto->SecurityScreen_Vehicles));
        query.addBindValue(QVariant(pto->SecurityScreen_FirstAid));
        query.addBindValue(QVariant(pto->SecurityScreen_Communications));
        query.addBindValue(QVariant(pto->SecurityScreen_Ids));
        query.addBindValue(QVariant(pto->StaffScreen_ProdStaff));
        query.addBindValue(QVariant(pto->StaffScreen_StageHands));
        query.addBindValue(QVariant(pto->TicketComm));
        query.addBindValue(QVariant(pto->ProductionName));
        query.addBindValue(QVariant(pto->ProductionID));

        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::InsertProduction(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("INSERT INTO Productions (Main_lblSize ,Main_lblVenues ,Main_lblArtists ,Main_lblPerformaceDates ,Main_lblProdCost ,Main_lblStaffCost ,Main_lblAdminCosts ,Main_lblMktgCosts ,Main_lblSecCosts ,Main_lblOpsCost ,Main_lblSubTotal ,Main_TicketCount ,Main_lblTicketSalesPos ,Main_lblPossibleDollars ,Main_lblPossibleTicletCOmm ,Main_lblTax ,Main_lblNet ,AdminScreen_AdminStaff ,AdminScreen_BoxOffStaff ,AdminScreen_TicketTakers ,AdminScreen_TrafficControl ,AdminScreen_OfficeCosts ,AdminScreen_OfficeEquipment ,AdminScreen_Postage ,AdminScreen_Phones ,AdminScreen_LegalAccountant ,AdminScreen_RentPower ,AdminScreen_Insurance ,AdminScreen_Permits ,AdminScreen_ASCAP ,AdminScreen_Ticketprinting ,MarketingScreen_PublicRelations ,MarketingScreen_CreativePrinting ,MarketingScreen_MktgStaff ,OperationsScreen_SiteOpsCoordinator ,OperationsScreen_Laborers ,OperationsScreen_VenueCost ,OperationsScreen_LandRestore ,OperationsScreen_Kamode ,OperationsScreen_WaterTrucks ,OperationsScreen_EquipmentRent ,OperationsScreen_Petroleum ,OperationsScreen_PowerGenerator ,OperationsScreen_PerimeterLights ,OperationsScreen_Fence ,OperationsScreen_OpsVehicles ,OperationsScreen_ShuttleBuses ,OperationsScreen_MotorHomes ,OperationsScreen_LandPrep ,ProductionScreen_Talent ,ProductionScreen_Sound ,ProductionScreen_Lights ,ProductionScreen_Catering ,ProductionScreen_Stage ,ProductionScreen_LightTowers ,ProductionScreen_Aerials ,SecurityScreen_SecStaff ,SecurityScreen_Vehicles ,SecurityScreen_FirstAid ,SecurityScreen_Communications ,SecurityScreen_Ids ,StaffScreen_ProdStaff ,StaffScreen_StageHands ,TicketComm ,ProductionName)values(? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,?)");
        query.addBindValue(QVariant(pto->Main_lblSize));
        query.addBindValue(QVariant(pto->Main_lblVenues));
        query.addBindValue(QVariant(pto->Main_lblArtists));
        query.addBindValue(QVariant(pto->Main_lblPerformaceDates));
        query.addBindValue(QVariant(pto->Main_lblProdCost));
        query.addBindValue(QVariant(pto->Main_lblStaffCost));
        query.addBindValue(QVariant(pto->Main_lblAdminCosts));
        query.addBindValue(QVariant(pto->Main_lblMktgCosts));
        query.addBindValue(QVariant(pto->Main_lblSecCosts));
        query.addBindValue(QVariant(pto->Main_lblOpsCost));
        query.addBindValue(QVariant(pto->Main_lblSubTotal));
        query.addBindValue(QVariant(pto->Main_TicketCount));
        query.addBindValue(QVariant(pto->Main_lblTicketSalesPos));
        query.addBindValue(QVariant(pto->Main_lblPossibleDollars));
        query.addBindValue(QVariant(pto->Main_lblPossibleTicletCOmm));
        query.addBindValue(QVariant(pto->Main_lblTax));
        query.addBindValue(QVariant(pto->Main_lblNet));
        query.addBindValue(QVariant(pto->AdminScreen_AdminStaff));
        query.addBindValue(QVariant(pto->AdminScreen_BoxOffStaff));
        query.addBindValue(QVariant(pto->AdminScreen_TicketTakers));
        query.addBindValue(QVariant(pto->AdminScreen_TrafficControl));
        query.addBindValue(QVariant(pto->AdminScreen_OfficeCosts));
        query.addBindValue(QVariant(pto->AdminScreen_OfficeEquipment));
        query.addBindValue(QVariant(pto->AdminScreen_Postage));
        query.addBindValue(QVariant(pto->AdminScreen_Phones));
        query.addBindValue(QVariant(pto->AdminScreen_LegalAccountant));
        query.addBindValue(QVariant(pto->AdminScreen_RentPower));
        query.addBindValue(QVariant(pto->AdminScreen_Insurance));
        query.addBindValue(QVariant(pto->AdminScreen_Permits));
        query.addBindValue(QVariant(pto->AdminScreen_ASCAP));
        query.addBindValue(QVariant(pto->AdminScreen_Ticketprinting));
        query.addBindValue(QVariant(pto->MarketingScreen_PublicRelations));
        query.addBindValue(QVariant(pto->MarketingScreen_CreativePrinting));
        query.addBindValue(QVariant(pto->MarketingScreen_MktgStaff));
        query.addBindValue(QVariant(pto->OperationsScreen_SiteOpsCoordinator));
        query.addBindValue(QVariant(pto->OperationsScreen_Laborers));
        query.addBindValue(QVariant(pto->OperationsScreen_VenueCost));
        query.addBindValue(QVariant(pto->OperationsScreen_LandRestore));
        query.addBindValue(QVariant(pto->OperationsScreen_Kamode));
        query.addBindValue(QVariant(pto->OperationsScreen_WaterTrucks));
        query.addBindValue(QVariant(pto->OperationsScreen_EquipmentRent));
        query.addBindValue(QVariant(pto->OperationsScreen_Petroleum));
        query.addBindValue(QVariant(pto->OperationsScreen_PowerGenerator));
        query.addBindValue(QVariant(pto->OperationsScreen_PerimeterLights));
        query.addBindValue(QVariant(pto->OperationsScreen_Fence));
        query.addBindValue(QVariant(pto->OperationsScreen_OpsVehicles));
        query.addBindValue(QVariant(pto->OperationsScreen_ShuttleBuses));
        query.addBindValue(QVariant(pto->OperationsScreen_MotorHomes));
        query.addBindValue(QVariant(pto->OperationsScreen_LandPrep));
        query.addBindValue(QVariant(pto->ProductionScreen_Talent));
        query.addBindValue(QVariant(pto->ProductionScreen_Sound));
        query.addBindValue(QVariant(pto->ProductionScreen_Lights));
        query.addBindValue(QVariant(pto->ProductionScreen_Catering));
        query.addBindValue(QVariant(pto->ProductionScreen_Stage));
        query.addBindValue(QVariant(pto->ProductionScreen_LightTowers));
        query.addBindValue(QVariant(pto->ProductionScreen_Aerials));
        query.addBindValue(QVariant(pto->SecurityScreen_SecStaff));
        query.addBindValue(QVariant(pto->SecurityScreen_Vehicles));
        query.addBindValue(QVariant(pto->SecurityScreen_FirstAid));
        query.addBindValue(QVariant(pto->SecurityScreen_Communications));
        query.addBindValue(QVariant(pto->SecurityScreen_Ids));
        query.addBindValue(QVariant(pto->StaffScreen_ProdStaff));
        query.addBindValue(QVariant(pto->StaffScreen_StageHands));
        query.addBindValue(QVariant(pto->TicketComm));
        query.addBindValue(QVariant(pto->ProductionName));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectVenuesByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("SELECT Production_ProductionID, VenueName,VenueAddress1,VenueAddress2,VenueAddress3,VenueCity,VenueState,VenuePostalCode,VenueSpecialNotes FROM Venues WHERE Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();

        pto->Venues->clear();

        while (query.next())
        {
            Venue *a = new Venue;

            a->VenueName = query.value(1).toString();
            a->VenueAddress1 = query.value(2).toString();
            a->VenueAddress2 = query.value(3).toString();
            a->VenueAddress3 = query.value(4).toString();
            a->VenueCity = query.value(5).toString();
            a->VenueState = query.value(6).toString();
            a->VenuePostalCode = query.value(7).toString();
            a->VenueSpecialNotes = query.value(8).toString();

            pto->Venues->append(a);
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectTicketsByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("select Cost, NumberAvailable, Production_ProductionID, TierDescription, TierName from Tickets where Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();

        pto->TicketTiers->clear();

        while(query.next())
        {
            Ticket *t = new Ticket;
            t->Cost = query.value(0).toDouble();
            t->NumberAvailable = query.value(1).toInt();
            t->TierDescription = query.value(3).toString();
            t->TierName = query.value(4).toString();

            pto->TicketTiers->append(t);
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectTaxesByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("select TaxName, TaxRate, Production_ProductionID from Taxes where Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();

        pto->Taxes->clear();

        while(query.next())
        {
            Tax *t = new Tax;
            t->TaxName = query.value(0).toString();
            t->TaxRate = query.value(1).toDouble();
            pto->Taxes->append(t);
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectProductionByName(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("SELECT Main_lblSize ,Main_lblVenues ,Main_lblArtists ,Main_lblPerformaceDates ,Main_lblProdCost ,Main_lblStaffCost ,Main_lblAdminCosts ,Main_lblMktgCosts ,Main_lblSecCosts ,Main_lblOpsCost ,Main_lblSubTotal ,Main_TicketCount ,Main_lblTicketSalesPos ,Main_lblPossibleDollars ,Main_lblPossibleTicletCOmm ,Main_lblTax ,Main_lblNet ,AdminScreen_AdminStaff ,AdminScreen_BoxOffStaff ,AdminScreen_TicketTakers ,AdminScreen_TrafficControl ,AdminScreen_OfficeCosts ,AdminScreen_OfficeEquipment ,AdminScreen_Postage ,AdminScreen_Phones ,AdminScreen_LegalAccountant ,AdminScreen_RentPower ,AdminScreen_Insurance ,AdminScreen_Permits ,AdminScreen_ASCAP ,AdminScreen_Ticketprinting ,MarketingScreen_PublicRelations ,MarketingScreen_CreativePrinting ,MarketingScreen_MktgStaff ,OperationsScreen_SiteOpsCoordinator ,OperationsScreen_Laborers ,OperationsScreen_VenueCost ,OperationsScreen_LandRestore ,OperationsScreen_Kamode ,OperationsScreen_WaterTrucks ,OperationsScreen_EquipmentRent ,OperationsScreen_Petroleum ,OperationsScreen_PowerGenerator ,OperationsScreen_PerimeterLights ,OperationsScreen_Fence ,OperationsScreen_OpsVehicles ,OperationsScreen_ShuttleBuses ,OperationsScreen_MotorHomes ,OperationsScreen_LandPrep ,ProductionScreen_Talent ,ProductionScreen_Sound ,ProductionScreen_Lights ,ProductionScreen_Catering ,ProductionScreen_Stage ,ProductionScreen_LightTowers ,ProductionScreen_Aerials ,SecurityScreen_SecStaff ,SecurityScreen_Vehicles ,SecurityScreen_FirstAid ,SecurityScreen_Communications ,SecurityScreen_Ids ,StaffScreen_ProdStaff ,StaffScreen_StageHands ,TicketComm ,ProductionName,ProductionID FROM Productions WHERE ProductionName = ?");
        query.addBindValue(QVariant(pto->ProductionName));
        query.exec();

        while(query.next())
        {
            pto->Main_lblSize = query.value(0).toString();
            pto->Main_lblVenues = query.value(1).toString();
            pto->Main_lblArtists = query.value(2).toString();
            pto->Main_lblPerformaceDates = query.value(3).toString();
            pto->Main_lblProdCost = query.value(4).toString();
            pto->Main_lblStaffCost = query.value(5).toString();
            pto->Main_lblAdminCosts = query.value(6).toString();
            pto->Main_lblMktgCosts = query.value(7).toString();
            pto->Main_lblSecCosts = query.value(8).toString();
            pto->Main_lblOpsCost = query.value(9).toString();
            pto->Main_lblSubTotal = query.value(10).toString();
            pto->Main_TicketCount = query.value(11).toString();
            pto->Main_lblTicketSalesPos = query.value(12).toString();
            pto->Main_lblPossibleDollars = query.value(13).toString();
            pto->Main_lblPossibleTicletCOmm = query.value(14).toString();
            pto->Main_lblTax = query.value(15).toString();
            pto->Main_lblNet = query.value(16).toString();
            pto->AdminScreen_AdminStaff = query.value(17).toDouble();
            pto->AdminScreen_BoxOffStaff = query.value(18).toDouble();
            pto->AdminScreen_TicketTakers = query.value(19).toDouble();
            pto->AdminScreen_TrafficControl = query.value(20).toDouble();
            pto->AdminScreen_OfficeCosts = query.value(21).toDouble();
            pto->AdminScreen_OfficeEquipment = query.value(22).toDouble();
            pto->AdminScreen_Postage = query.value(23).toDouble();
            pto->AdminScreen_Phones = query.value(24).toDouble();
            pto->AdminScreen_LegalAccountant = query.value(25).toDouble();
            pto->AdminScreen_RentPower = query.value(26).toDouble();
            pto->AdminScreen_Insurance = query.value(27).toDouble();
            pto->AdminScreen_Permits = query.value(28).toDouble();
            pto->AdminScreen_ASCAP = query.value(29).toDouble();
            pto->AdminScreen_Ticketprinting = query.value(30).toDouble();
            pto->MarketingScreen_PublicRelations = query.value(31).toDouble();
            pto->MarketingScreen_CreativePrinting = query.value(32).toDouble();
            pto->MarketingScreen_MktgStaff = query.value(33).toDouble();
            pto->OperationsScreen_SiteOpsCoordinator = query.value(34).toDouble();
            pto->OperationsScreen_Laborers = query.value(35).toDouble();
            pto->OperationsScreen_VenueCost = query.value(36).toDouble();
            pto->OperationsScreen_LandRestore = query.value(37).toDouble();
            pto->OperationsScreen_Kamode = query.value(38).toDouble();
            pto->OperationsScreen_WaterTrucks = query.value(39).toDouble();
            pto->OperationsScreen_EquipmentRent = query.value(40).toDouble();
            pto->OperationsScreen_Petroleum = query.value(41).toDouble();
            pto->OperationsScreen_PowerGenerator = query.value(42).toDouble();
            pto->OperationsScreen_PerimeterLights = query.value(43).toDouble();
            pto->OperationsScreen_Fence = query.value(44).toDouble();
            pto->OperationsScreen_OpsVehicles = query.value(45).toDouble();
            pto->OperationsScreen_ShuttleBuses = query.value(46).toDouble();
            pto->OperationsScreen_MotorHomes = query.value(47).toDouble();
            pto->OperationsScreen_LandPrep = query.value(48).toDouble();
            pto->ProductionScreen_Talent = query.value(49).toDouble();
            pto->ProductionScreen_Sound = query.value(50).toDouble();
            pto->ProductionScreen_Lights = query.value(51).toDouble();
            pto->ProductionScreen_Catering = query.value(52).toDouble();
            pto->ProductionScreen_Stage = query.value(53).toDouble();
            pto->ProductionScreen_LightTowers = query.value(54).toDouble();
            pto->ProductionScreen_Aerials = query.value(55).toDouble();
            pto->SecurityScreen_SecStaff = query.value(56).toDouble();
            pto->SecurityScreen_Vehicles = query.value(57).toDouble();
            pto->SecurityScreen_FirstAid = query.value(58).toDouble();
            pto->SecurityScreen_Communications = query.value(59).toDouble();
            pto->SecurityScreen_Ids = query.value(60).toDouble();
            pto->StaffScreen_ProdStaff = query.value(61).toDouble();
            pto->StaffScreen_StageHands = query.value(62).toDouble();
            pto->TicketComm = query.value(63).toDouble();
            pto->ProductionName = query.value(64).toString();
            pto->ProductionID = query.value(65).toInt();
        }

        this->CloseConnection(qd);

        this->SelectArtistByProductionID(pto, resp);
        this->SelectTaxesByProductionID(pto, resp);
        this->SelectTicketsByProductionID(pto, resp);
        this->SelectVenuesByProductionID(pto, resp);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectProdctionByID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("SELECT Main_lblSize ,Main_lblVenues ,Main_lblArtists ,Main_lblPerformaceDates ,Main_lblProdCost ,Main_lblStaffCost ,Main_lblAdminCosts ,Main_lblMktgCosts ,Main_lblSecCosts ,Main_lblOpsCost ,Main_lblSubTotal ,Main_TicketCount ,Main_lblTicketSalesPos ,Main_lblPossibleDollars ,Main_lblPossibleTicletCOmm ,Main_lblTax ,Main_lblNet ,AdminScreen_AdminStaff ,AdminScreen_BoxOffStaff ,AdminScreen_TicketTakers ,AdminScreen_TrafficControl ,AdminScreen_OfficeCosts ,AdminScreen_OfficeEquipment ,AdminScreen_Postage ,AdminScreen_Phones ,AdminScreen_LegalAccountant ,AdminScreen_RentPower ,AdminScreen_Insurance ,AdminScreen_Permits ,AdminScreen_ASCAP ,AdminScreen_Ticketprinting ,MarketingScreen_PublicRelations ,MarketingScreen_CreativePrinting ,MarketingScreen_MktgStaff ,OperationsScreen_SiteOpsCoordinator ,OperationsScreen_Laborers ,OperationsScreen_VenueCost ,OperationsScreen_LandRestore ,OperationsScreen_Kamode ,OperationsScreen_WaterTrucks ,OperationsScreen_EquipmentRent ,OperationsScreen_Petroleum ,OperationsScreen_PowerGenerator ,OperationsScreen_PerimeterLights ,OperationsScreen_Fence ,OperationsScreen_OpsVehicles ,OperationsScreen_ShuttleBuses ,OperationsScreen_MotorHomes ,OperationsScreen_LandPrep ,ProductionScreen_Talent ,ProductionScreen_Sound ,ProductionScreen_Lights ,ProductionScreen_Catering ,ProductionScreen_Stage ,ProductionScreen_LightTowers ,ProductionScreen_Aerials ,SecurityScreen_SecStaff ,SecurityScreen_Vehicles ,SecurityScreen_FirstAid ,SecurityScreen_Communications ,SecurityScreen_Ids ,StaffScreen_ProdStaff ,StaffScreen_StageHands ,TicketComm ,ProductionName,ProductionID FROM Productions WHERE ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();

        while(query.next())
        {
            pto->Main_lblSize = query.value(0).toString();
            pto->Main_lblVenues = query.value(1).toString();
            pto->Main_lblArtists = query.value(2).toString();
            pto->Main_lblPerformaceDates = query.value(3).toString();
            pto->Main_lblProdCost = query.value(4).toString();
            pto->Main_lblStaffCost = query.value(5).toString();
            pto->Main_lblAdminCosts = query.value(6).toString();
            pto->Main_lblMktgCosts = query.value(7).toString();
            pto->Main_lblSecCosts = query.value(8).toString();
            pto->Main_lblOpsCost = query.value(9).toString();
            pto->Main_lblSubTotal = query.value(10).toString();
            pto->Main_TicketCount = query.value(11).toString();
            pto->Main_lblTicketSalesPos = query.value(12).toString();
            pto->Main_lblPossibleDollars = query.value(13).toString();
            pto->Main_lblPossibleTicletCOmm = query.value(14).toString();
            pto->Main_lblTax = query.value(15).toString();
            pto->Main_lblNet = query.value(16).toString();
            pto->AdminScreen_AdminStaff = query.value(17).toDouble();
            pto->AdminScreen_BoxOffStaff = query.value(18).toDouble();
            pto->AdminScreen_TicketTakers = query.value(19).toDouble();
            pto->AdminScreen_TrafficControl = query.value(20).toDouble();
            pto->AdminScreen_OfficeCosts = query.value(21).toDouble();
            pto->AdminScreen_OfficeEquipment = query.value(22).toDouble();
            pto->AdminScreen_Postage = query.value(23).toDouble();
            pto->AdminScreen_Phones = query.value(24).toDouble();
            pto->AdminScreen_LegalAccountant = query.value(25).toDouble();
            pto->AdminScreen_RentPower = query.value(26).toDouble();
            pto->AdminScreen_Insurance = query.value(27).toDouble();
            pto->AdminScreen_Permits = query.value(28).toDouble();
            pto->AdminScreen_ASCAP = query.value(29).toDouble();
            pto->AdminScreen_Ticketprinting = query.value(30).toDouble();
            pto->MarketingScreen_PublicRelations = query.value(31).toDouble();
            pto->MarketingScreen_CreativePrinting = query.value(32).toDouble();
            pto->MarketingScreen_MktgStaff = query.value(33).toDouble();
            pto->OperationsScreen_SiteOpsCoordinator = query.value(34).toDouble();
            pto->OperationsScreen_Laborers = query.value(35).toDouble();
            pto->OperationsScreen_VenueCost = query.value(36).toDouble();
            pto->OperationsScreen_LandRestore = query.value(37).toDouble();
            pto->OperationsScreen_Kamode = query.value(38).toDouble();
            pto->OperationsScreen_WaterTrucks = query.value(39).toDouble();
            pto->OperationsScreen_EquipmentRent = query.value(40).toDouble();
            pto->OperationsScreen_Petroleum = query.value(41).toDouble();
            pto->OperationsScreen_PowerGenerator = query.value(42).toDouble();
            pto->OperationsScreen_PerimeterLights = query.value(43).toDouble();
            pto->OperationsScreen_Fence = query.value(44).toDouble();
            pto->OperationsScreen_OpsVehicles = query.value(45).toDouble();
            pto->OperationsScreen_ShuttleBuses = query.value(46).toDouble();
            pto->OperationsScreen_MotorHomes = query.value(47).toDouble();
            pto->OperationsScreen_LandPrep = query.value(48).toDouble();
            pto->ProductionScreen_Talent = query.value(49).toDouble();
            pto->ProductionScreen_Sound = query.value(50).toDouble();
            pto->ProductionScreen_Lights = query.value(51).toDouble();
            pto->ProductionScreen_Catering = query.value(52).toDouble();
            pto->ProductionScreen_Stage = query.value(53).toDouble();
            pto->ProductionScreen_LightTowers = query.value(54).toDouble();
            pto->ProductionScreen_Aerials = query.value(55).toDouble();
            pto->SecurityScreen_SecStaff = query.value(56).toDouble();
            pto->SecurityScreen_Vehicles = query.value(57).toDouble();
            pto->SecurityScreen_FirstAid = query.value(58).toDouble();
            pto->SecurityScreen_Communications = query.value(59).toDouble();
            pto->SecurityScreen_Ids = query.value(60).toDouble();
            pto->StaffScreen_ProdStaff = query.value(61).toDouble();
            pto->StaffScreen_StageHands = query.value(62).toDouble();
            pto->TicketComm = query.value(63).toDouble();
            pto->ProductionName = query.value(64).toString();
            pto->ProductionID= query.value(65).toInt();
        }

        this->CloseConnection(qd);

        this->SelectArtistByProductionID(pto, resp);
        this->SelectTaxesByProductionID(pto, resp);
        this->SelectTicketsByProductionID(pto, resp);
        this->SelectVenuesByProductionID(pto, resp);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectArtistByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("SELECT ArtistName from ARTISTS WHERE ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();

        pto->Artists->clear();

        while (query.next())
        {
            Artist *a = new Artist;
            a->ArtistName = query.value(0).toString();
            pto->Artists->append(a);
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::SelectAllProductions(ComResponse *resp)
{
    bool retval = false;

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        QSqlQuery query(qd);
        query.prepare("SELECT Main_lblSize ,Main_lblVenues ,Main_lblArtists ,Main_lblPerformaceDates ,Main_lblProdCost ,Main_lblStaffCost ,Main_lblAdminCosts ,Main_lblMktgCosts ,Main_lblSecCosts ,Main_lblOpsCost ,Main_lblSubTotal ,Main_TicketCount ,Main_lblTicketSalesPos ,Main_lblPossibleDollars ,Main_lblPossibleTicletCOmm ,Main_lblTax ,Main_lblNet ,AdminScreen_AdminStaff ,AdminScreen_BoxOffStaff ,AdminScreen_TicketTakers ,AdminScreen_TrafficControl ,AdminScreen_OfficeCosts ,AdminScreen_OfficeEquipment ,AdminScreen_Postage ,AdminScreen_Phones ,AdminScreen_LegalAccountant ,AdminScreen_RentPower ,AdminScreen_Insurance ,AdminScreen_Permits ,AdminScreen_ASCAP ,AdminScreen_Ticketprinting ,MarketingScreen_PublicRelations ,MarketingScreen_CreativePrinting ,MarketingScreen_MktgStaff ,OperationsScreen_SiteOpsCoordinator ,OperationsScreen_Laborers ,OperationsScreen_VenueCost ,OperationsScreen_LandRestore ,OperationsScreen_Kamode ,OperationsScreen_WaterTrucks ,OperationsScreen_EquipmentRent ,OperationsScreen_Petroleum ,OperationsScreen_PowerGenerator ,OperationsScreen_PerimeterLights ,OperationsScreen_Fence ,OperationsScreen_OpsVehicles ,OperationsScreen_ShuttleBuses ,OperationsScreen_MotorHomes ,OperationsScreen_LandPrep ,ProductionScreen_Talent ,ProductionScreen_Sound ,ProductionScreen_Lights ,ProductionScreen_Catering ,ProductionScreen_Stage ,ProductionScreen_LightTowers ,ProductionScreen_Aerials ,SecurityScreen_SecStaff ,SecurityScreen_Vehicles ,SecurityScreen_FirstAid ,SecurityScreen_Communications ,SecurityScreen_Ids ,StaffScreen_ProdStaff ,StaffScreen_StageHands ,TicketComm ,ProductionName,ProductionID FROM Productions");
        query.exec();

        this->PTList->clear();

        while(query.next())
        {
            ProtoolsStandard *pto = new ProtoolsStandard;
            pto->Main_lblSize = query.value(0).toString();
            pto->Main_lblVenues = query.value(1).toString();
            pto->Main_lblArtists = query.value(2).toString();
            pto->Main_lblPerformaceDates = query.value(3).toString();
            pto->Main_lblProdCost = query.value(4).toString();
            pto->Main_lblStaffCost = query.value(5).toString();
            pto->Main_lblAdminCosts = query.value(6).toString();
            pto->Main_lblMktgCosts = query.value(7).toString();
            pto->Main_lblSecCosts = query.value(8).toString();
            pto->Main_lblOpsCost = query.value(9).toString();
            pto->Main_lblSubTotal = query.value(10).toString();
            pto->Main_TicketCount = query.value(11).toString();
            pto->Main_lblTicketSalesPos = query.value(12).toString();
            pto->Main_lblPossibleDollars = query.value(13).toString();
            pto->Main_lblPossibleTicletCOmm = query.value(14).toString();
            pto->Main_lblTax = query.value(15).toString();
            pto->Main_lblNet = query.value(16).toString();
            pto->AdminScreen_AdminStaff = query.value(17).toDouble();
            pto->AdminScreen_BoxOffStaff = query.value(18).toDouble();
            pto->AdminScreen_TicketTakers = query.value(19).toDouble();
            pto->AdminScreen_TrafficControl = query.value(20).toDouble();
            pto->AdminScreen_OfficeCosts = query.value(21).toDouble();
            pto->AdminScreen_OfficeEquipment = query.value(22).toDouble();
            pto->AdminScreen_Postage = query.value(23).toDouble();
            pto->AdminScreen_Phones = query.value(24).toDouble();
            pto->AdminScreen_LegalAccountant = query.value(25).toDouble();
            pto->AdminScreen_RentPower = query.value(26).toDouble();
            pto->AdminScreen_Insurance = query.value(27).toDouble();
            pto->AdminScreen_Permits = query.value(28).toDouble();
            pto->AdminScreen_ASCAP = query.value(29).toDouble();
            pto->AdminScreen_Ticketprinting = query.value(30).toDouble();
            pto->MarketingScreen_PublicRelations = query.value(31).toDouble();
            pto->MarketingScreen_CreativePrinting = query.value(32).toDouble();
            pto->MarketingScreen_MktgStaff = query.value(33).toDouble();
            pto->OperationsScreen_SiteOpsCoordinator = query.value(34).toDouble();
            pto->OperationsScreen_Laborers = query.value(35).toDouble();
            pto->OperationsScreen_VenueCost = query.value(36).toDouble();
            pto->OperationsScreen_LandRestore = query.value(37).toDouble();
            pto->OperationsScreen_Kamode = query.value(38).toDouble();
            pto->OperationsScreen_WaterTrucks = query.value(39).toDouble();
            pto->OperationsScreen_EquipmentRent = query.value(40).toDouble();
            pto->OperationsScreen_Petroleum = query.value(41).toDouble();
            pto->OperationsScreen_PowerGenerator = query.value(42).toDouble();
            pto->OperationsScreen_PerimeterLights = query.value(43).toDouble();
            pto->OperationsScreen_Fence = query.value(44).toDouble();
            pto->OperationsScreen_OpsVehicles = query.value(45).toDouble();
            pto->OperationsScreen_ShuttleBuses = query.value(46).toDouble();
            pto->OperationsScreen_MotorHomes = query.value(47).toDouble();
            pto->OperationsScreen_LandPrep = query.value(48).toDouble();
            pto->ProductionScreen_Talent = query.value(49).toDouble();
            pto->ProductionScreen_Sound = query.value(50).toDouble();
            pto->ProductionScreen_Lights = query.value(51).toDouble();
            pto->ProductionScreen_Catering = query.value(52).toDouble();
            pto->ProductionScreen_Stage = query.value(53).toDouble();
            pto->ProductionScreen_LightTowers = query.value(54).toDouble();
            pto->ProductionScreen_Aerials = query.value(55).toDouble();
            pto->SecurityScreen_SecStaff = query.value(56).toDouble();
            pto->SecurityScreen_Vehicles = query.value(57).toDouble();
            pto->SecurityScreen_FirstAid = query.value(58).toDouble();
            pto->SecurityScreen_Communications = query.value(59).toDouble();
            pto->SecurityScreen_Ids = query.value(60).toDouble();
            pto->StaffScreen_ProdStaff = query.value(61).toDouble();
            pto->StaffScreen_StageHands = query.value(62).toDouble();
            pto->TicketComm = query.value(63).toDouble();
            pto->ProductionName = query.value(64).toString();
            pto->ProductionID= query.value(65).toInt();

            this->SelectArtistByProductionID(pto, resp);
            this->SelectTaxesByProductionID(pto, resp);
            this->SelectTicketsByProductionID(pto, resp);
            this->SelectVenuesByProductionID(pto, resp);

            this->PTList->append(pto);
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::InsertVenue(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        for(int i = 0; i < pto->Venues->count(); i++)
        {
            qd.transaction();
            QSqlQuery query(qd);
            query.prepare("INSERT INTO Venues (Production_ProductionID, VenueName,VenueAddress1,VenueAddress2,VenueAddress3,VenueCity,VenueState,VenuePostalCode,VenueSpecialNotes) values (?, ?,?,?,?,?,?,?,?)");
            query.addBindValue(QVariant(pto->ProductionID));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueName));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueAddress1));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueAddress2));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueAddress3));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueCity));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueState));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenuePostalCode));
            query.addBindValue(QVariant(pto->Venues->at(i)->VenueSpecialNotes));
            query.exec();
            qd.commit();
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::InsertTicket(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        for(int i = 0; i < pto->TicketTiers->count(); i++)
        {
            qd.transaction();
            QSqlQuery query(qd);
            query.prepare("INSERT INTO Tickets (Cost, NumberAvailable, Production_ProductionID, TierDescription, TierName) values (?,?,?,?,?)");
            query.addBindValue(QVariant(pto->TicketTiers->at(i)->Cost));
            query.addBindValue(QVariant(pto->TicketTiers->at(i)->NumberAvailable));
            query.addBindValue(QVariant(pto->ProductionID));
            query.addBindValue(QVariant(pto->TicketTiers->at(i)->TierDescription));
            query.addBindValue(QVariant(pto->TicketTiers->at(i)->TierName));
            query.exec();
            qd.commit();
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::InsertTax(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        for (int i = 0; i < pto->Taxes->count(); i++)
        {
            qd.transaction();
            QSqlQuery query(qd);
            query.prepare("INSERT INTO TAXES (TaxName, TaxRate, Production_ProductionID) VALUES (?,?,?)");
            query.addBindValue(QVariant(pto->Taxes->at(i)->TaxName));
            query.addBindValue(QVariant(pto->Taxes->at(i)->TaxRate));
            query.addBindValue(QVariant(pto->ProductionID));
            query.exec();
            qd.commit();
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::DeleteArtistByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("DELETE from ARTISTS where Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::DeleteProductionByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("DELETE FROM Productions where ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::DeleteTaxByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("Delete from Taxes where Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::DeleteTicketByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("Delete from Tickets where Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::DeleteVenueByProductionID(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        qd.transaction();
        QSqlQuery query(qd);
        query.prepare("Delete from Venues where Production_ProductionID = ?");
        query.addBindValue(QVariant(pto->ProductionID));
        query.exec();
        qd.commit();
        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}

bool DataBaseFacade::InsertArtists(ProtoolsStandard *pto, ComResponse *resp)
{
    bool retval = false;

    if (!pto)
    {
        return retval;
    }

    if (!resp)
    {
        resp = new ComResponse;
    }

    QSqlDatabase qd;

    if (this->OpenConnection(qd))
    {
        for (int i = 0; i < pto->Artists->count(); i++)
        {
            qd.transaction();
            QSqlQuery query(qd);
            query.prepare("INSERT INTO Artists (ArtistName, Productions_ProductionID) VALUES (?, ?)");
            query.addBindValue(QVariant(pto->Artists->at(i)->ArtistName));
            query.addBindValue(QVariant(pto->ProductionID));
            query.exec();
            qd.commit();
        }

        this->CloseConnection(qd);
    }
    else
    {
        retval = false;
    }

    return retval;
}
